const bodyParser = require('body-parser')
var express = require('express')
var path = require('path')
var session = require('express-session')

var router = require('./router')

var app = express()

app.use('/public/', express.static(path.join(__dirname, './public/')))
app.use('/node_modules/', express.static(path.join(__dirname, './node_modules/')))

app.engine('html', require('express-art-template'))
app.set('views', path.join(__dirname, './views/'))

app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true
}))

//把路由挂在到app中
app.use(router)

app.use(function(req,res){
    res.render('404.html')
})

app.use(function(err,req,res,next){
    console.log(err);
    res.status(500).json({
        err_code: 500,
        message: err.message
    })
})

app.listen(3000, function () {
    console.log('Server is running...');
})